package com.fenghongzhang.lib_jetpack.recyclerview

import android.graphics.Canvas
import android.os.Handler
import android.os.Looper
import android.view.View
import android.widget.Button
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import com.fenghongzhang.lib_jetpack.R

class ItemTouchHelperCallback(
    private val adapter: MultiTypeAdapter
) : ItemTouchHelper.Callback() {
    private var isSwiped = false // 标记是否已经滑动
    private var swipeThreshold = 0.5f // 设置滑动阈值，50%滑动时显示删除按钮
    override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int {
        // 支持拖动（上下移动）和滑动（左右滑动）
        val dragFlags = ItemTouchHelper.UP or ItemTouchHelper.DOWN
        val swipeFlags = ItemTouchHelper.START  // 只允许左滑删除
        return makeMovementFlags(dragFlags, swipeFlags)
    }

    override fun onMove(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        target: RecyclerView.ViewHolder
    ): Boolean {
        // 处理拖动排序的逻辑
        val fromPosition = viewHolder.adapterPosition
        val toPosition = target.adapterPosition
        adapter.onItemMove(fromPosition, toPosition)
        return true
    }

    override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
        // 处理左滑删除的逻辑
        if (direction == ItemTouchHelper.START) {
            when (viewHolder) {
                is MultiTypeAdapter.Type1ViewHolder -> {
                    val position = viewHolder.adapterPosition
                }
                is MultiTypeAdapter.Type2ViewHolder -> {
                    val position = viewHolder.adapterPosition
                    // 延迟删除项，提供用户交互时间
                    Handler(Looper.getMainLooper()).postDelayed({
                        adapter.onItemSwiped(position)
                    }, 1000) // 延迟 1 秒钟删除，用户可以看到删除按钮

                }
                // 你可以根据需求为其他类型的 ViewHolder 处理不同的操作
            }
        }
    }


    override fun onChildDraw(
        c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder,
        dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean
    ) {
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)

        // 只有在滑动时才显示删除按钮
        if (viewHolder is MultiTypeAdapter.Type1ViewHolder) {
            val deleteButton = viewHolder.itemView.findViewById<Button>(R.id.deleteButton1)

            if (isCurrentlyActive) {
                println(Math.abs(dX).toInt())
              //  println(viewHolder.itemView.width * swipeThreshold)
                // 如果滑动距离达到阈值，显示删除按钮
                if (Math.abs(dX).toInt() >= 100) {
                    viewHolder.showDeleteButton()
                    // 停止滑动，将 item 停止在当前位置
                    viewHolder.itemView.translationX = -100f
                    deleteButton.translationX = 0f
                } else {
                    viewHolder.hideDeleteButton()
                    // 继续滑动，直到达到阈值
                // viewHolder.itemView.translationX = dX
                }
            } else {
                viewHolder.showDeleteButton() // 当滑动未进行时，隐藏按钮
                viewHolder.itemView.translationX = -100f
            }
        }
    }

    override fun isLongPressDragEnabled(): Boolean = true  // 启用长按拖动
    override fun isItemViewSwipeEnabled(): Boolean = true  // 启用滑动操作
}
